/**
 * 
 */
package dbAccess;

import java.sql.*;

import model.ConnectionSettings;
import exceptions.*;

/**
 * @author Anthares & Hennek
 *
 */
public class DBConnexionSingleton {
	public static Connection uniqueInstance;
	private static ConnectionSettings connSet;
	
	public static Connection getInstance() throws PasIdentificationException{
		if (uniqueInstance == null) throw new PasIdentificationException();
		else return uniqueInstance;
	}
	
	public static void identification(ConnectionSettings connSet) throws DatabaseErrorException, ODBCErrorException{
		if (uniqueInstance == null) {
			try {
				DBConnexionSingleton.connSet=connSet;
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				uniqueInstance = DriverManager.getConnection("jdbc:mysql://"+connSet.getDbAdress()+":"+connSet.getDbPort()+"/syllabus", connSet.getDbLogin(), connSet.getDbPass());
				uniqueInstance.setAutoCommit(false);
			} catch (SQLException e) {
				throw new DatabaseErrorException(e);
			} catch (Exception e) {
				throw new ODBCErrorException(e);
			}
		}
	}
	
	public static void logout() throws DatabaseErrorException{
		if(uniqueInstance!=null){
			try {
				uniqueInstance.close();
			} catch (SQLException e) {
				throw new DatabaseErrorException(e);
			}
		}
	}
}
